<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function renderToAlias($str) {
    $str = str_replace(array('à', 'á', 'ạ', 'ả', 'ã', 'â', 'ầ', 'ấ', 'ậ', 'ẩ', 'ẫ', 'ă', 'ằ', 'ắ', 'ặ', 'ậ', 'ẳ', 'ẵ', 'á', 'À', 'Á', 'á', 'Ạ', 'Ả', 'Ã', 'Â', 'Ầ', 'Ấ', 'Ậ', 'Ẩ', 'Ẫ', 'Ă', 'Ằ', 'Ắ', 'Ặ', 'Ẳ', 'Ẵ'), 'a', $str);
    $str = str_replace(array('è', 'é', 'ẹ', 'ẻ', 'ẽ', 'ê', 'ề', 'ế', 'ệ', 'ể', 'ễ', 'È', 'É', 'Ẹ', 'Ẻ', 'Ẽ', 'Ê', 'Ề', 'Ế', 'Ệ', 'Ể', 'Ễ'), 'e', $str);
    $str = str_replace(array('ì', 'í', 'ị', 'ỉ', 'ĩ', 'Ì', 'Í', 'Ị', 'Ỉ', 'Ĩ'), 'i', $str);
    $str = str_replace(array('ò', 'ó', 'ọ', 'ỏ', 'ờ', 'õ', 'ô', 'ồ', 'ố', 'ộ', 'ổ', 'ỗ', 'ơ', 'ờ', 'ớ', 'ợ', 'ở', 'ỡ', 'Ò', 'Ó', 'Ọ', 'Ỏ', 'Õ', 'Ô', 'Ồ', 'Ố', 'Ộ', 'Ổ', 'Ỗ', 'Ơ', 'Ờ', 'Ớ', 'Ợ', 'Ở', 'Ỡ'), 'o', $str);
    $str = str_replace(array('ù', 'ú', 'ụ', 'ủ', 'ũ', 'ư', 'ừ', 'ứ', 'ự', 'ử', 'ư', 'ữ', 'Ù', 'Ú', 'Ụ', 'Ủ', 'Ũ', 'Ư', 'Ừ', 'Ứ', 'Ự', 'Ử', 'Ữ', 'ứ'), 'u', $str);
    $str = str_replace(array('ỳ', 'ý', 'ỵ', 'ỷ', 'ỹ', 'Ỳ', 'Ý', 'Ỵ', 'Ỷ', 'Ỹ'), 'y', $str);
    $str = str_replace(array('đ', 'Đ'), 'd', $str);
    $str = str_replace(array('--', '...', '“', '”', '‘', '(TT&VH)-', '(PL&XH)- ', '(PL&XH) - ', '(Dân trí) - ', '(', '"', ')', '&', '!', '?', ':', '.', '…', '[', ']', '+', '*', '@', '^', '#', '$', '=', '~', '`', '’', '♥'), '', $str);
    $str = str_replace('%', '-phan-tram-', trim($str));
    $str = str_replace(array('---', '--', '- ', ' -', '/', ',', '  ', ' ', ';', ','), '-', $str);
    return strtolower($str);
}

function render_url($url) {
    if ($url[strlen($url) - 1] != '/') {
        $url .= '/';
    }
    return $url;
}

function render_site_url($url) {
    return BASE_URL . $url . '.html';
}

function renderTextHighlight($source, $keywords, $class) {
    return str_replace($keywords, '<span class="' . $class . '">' . $keywords . '</span>', $source);
}

function toCelsius($deg) {
    return ($deg - 32) / 1.8;
}

function render_desc($desc) {
    $desc = strip_tags($desc);
    if (strstr($desc, ">")) {
        $desc = substr($desc, 0, stripos($desc, '>'));
    }
    return $desc;
}

function render_tag($tags) {
    $tags = explode(',', $tags);
    if (is_string($tags)) {
        $tags = array($tags);
    }
    for ($index = 0; $index < count($tags); $index++) {
        $tags[$index] = trim($tags[$index]);
    }
    $tags = array_filter($tags);
    return $tags;
}

function render_src($image) {
    $image = str_replace('./', base_url() . '', $image);
    return $image;
}

function render_thumb($image) {
    if (!file_exists($image)) {
        return null;
    }
    $pathInfo = pathinfo($image);
    $thumbFile = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '_thumb.' . $pathInfo['extension'];
    if (!file_exists($thumbFile)) {
        $size = @getimagesize($image);
        if (!$size) {
            return render_src($image);
        }
        $percent = (float) $size[0] / (float) $size[1];
        $config['image_library'] = 'gd2';
        $config['source_image'] = $image;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = ARTICLE_IMAGE_THUMB_WIDTH;
        $config['height'] = floor($config['width'] / $percent);
        $CI = & get_instance();
        if (!isset($CI->image_lib)) {
            $CI->load->library('image_lib');
        }
        $CI->image_lib->initialize($config);
        @$CI->image_lib->resize();
        clearstatcache();
    }
    if (!file_exists($thumbFile)) {
        return render_src($image);
    }
    return render_src($thumbFile);
}

function format_price($number) {
    $number = intval(str_replace(array('VNĐ', '.'), '', $number));
    if ($number >= 1000) {
        return number_format($number, 0, ',', '.');
    }
    return $number;
}

function sub_price($first_price, $last_price) {
    $first_price = intval($first_price);
    $last_price = intval($last_price);
    $number = $first_price - $last_price;
    $data['percent'] = floor(($number * 100)/$first_price);
    if ($number >= 1000) {
        $number = number_format($number, 0, ',', '.');
    }
    $data['number'] = $number;
    return $data;
}

function get_field($item, $field) {
    if (is_object($item) && isset($item->$field)) {
        return $item->$field;
    }
    if (is_array($item) && isset($item[$field])) {
        return $item[$field];
    }
    return null;
}

function analyze_filters($filters) {
    $conds = array();
    foreach ($filters as $key => $field) {
        if (count($field) < 2 || !isset($field['value']) || empty($field['value'])) {
            continue;
        }
        $value = trim($field['value']);
        if (isset($field['exclude']) && $field['exclude'] == $field['value']) {
            continue;
        }
        switch ($field['type']) {
            case 'equal':
                $conds[$key] = $key . ' = "' . $value . '"';
                break;
            case 'text':
                $conds[$key] = $key . ' LIKE "%' . $value . '%"';
                break;
            case 'range':
                if (strstr($value, '-')) {
                    $segments = explode('-', $value);
                    if (count($segments) == 2) {
                        $conds[$key] = $key . ' >= ' . $segments[0] . ' AND ' . $key . ' <= ' . $segments[1];
                    }
                } else {
                    $conds[$key] = $key . ' = ' . $value;
                }
                break;
            default:
                break;
        }
    }
    if (count($conds) == 0) {
        $conds = null;
    }
    return $conds;
}

function no_image_src() {
    return BASE_URL . DEFAULT_NO_IMAGE;
}

function get_default_index($field, $index, $default_value) {
    if (empty($field[$index])) {
        return $default_value;
    }
    return $field[$index];
}

function get_default_attribute($field, $attribute, $default_value) {
    if (empty($field->$attribute)) {
        return $default_value;
    }
    return $field->$attribute;
}

function get_default($field, $default_value) {
    if (empty($field)) {
        return $default_value;
    }
    return $field;
}

function render_content($content) {
    $content = strip_tags($content, '<frame><iframe><font><a><br><hr><table><tbody><thead><tr><td><img><p><b><strong><h2><h3><h4><h5><object><embed><param><span>');
    $content = trim($content);
    return $content;
}

function format_currency($number)
{
    if ($number >= 1000) {
        return number_format($number, 0, ',', '.');
    }
    return $number;
}

function aasort(&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

function get_current_url()
{
    $pageURL = 'http';
    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
        $pageURL .= "s";
    }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

function sub_string($str, $start, $num, $ext=FALSE) {
    //$str = EnBacLib::plainText($str);

    if ($num > strlen($str) - $start)
        $num = strlen($str) - $start;

    if ($num <= 0)
        return '';

    $pos = strpos(substr($str, $start + $num + 1, strlen($str) - $start - $num - 1), ' ');

    $st = substr($str, $start, $num + $pos + 1);

    if ($ext && strlen($str) - $start > $num)
        $st.='..';

    return $st;
}
?>
